package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.dto.UserRoleDto;
import com.codingfuture.erp_system.entity.Emp;
import com.codingfuture.erp_system.service.UserRoleService;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/v2")
@RestController
public class UserRoleController {

    @Autowired
    private UserRoleService userRoleService;

    /**
     * 用户角色设置
     * 渲染角色
     *
     * @param page
     * @param rows
     * @return 角色数据
     */
    @PostMapping("/emp_list")
    public Result findByAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows) {
        Page<Object> page1 = PageHelper.startPage(page, rows);
        List<Emp> list = userRoleService.findByAll();
        PageData<Emp> data = new PageData<>(list, page1.getTotal());
        return Result.ok(data);
    }

    /**
     * 获取权限
     *
     * @param id
     * @return
     */
    @PostMapping("emp_readEmpRoles")
    public Result readEmpRoles(Integer id) {
        List<UserRoleDto> userRoleDtos = userRoleService.readEmpRolesById(id);
        List<UserRoleDto> userRoleDtoList = userRoleService.readEmpRolesByAll();
        for (UserRoleDto userRoleDto : userRoleDtos) {
            for (UserRoleDto roleDto : userRoleDtoList) {
                if (userRoleDto.getId().equals(roleDto.getId())) {
                    roleDto.setChecked(true);
                }
            }
        }

        return Result.OK(userRoleDtoList);
    }

    @PostMapping("/emp_updateEmpRoles")
    public Result updateEmpRoles(int id, Integer[] checkedStr) {
        Boolean result = userRoleService.updateEmpRoles(id, checkedStr);
        return Result.ok();
    }

}
